require('Global');

const ANDROID_CLASS = 'org/cocos2dx/javascript/AppActivity';

var app = {};
var TdSdk = {};
if (window.jsb && cc.sys.os === cc.sys.OS_IOS) {

    app.callLogin = () => jsb.reflection.callStaticMethod('GameTools', 'callLogin');
    app.onLogout = () => jsb.reflection.callStaticMethod('GameTools', 'onLogout');
    app.userBind = () => jsb.reflection.callStaticMethod('GameTools', 'userBind');
    app.openGame = (game) => jsb.reflection.callStaticMethod('GameTools', 'openGame:', game);
    app.gameCharge = (data) => jsb.reflection.callStaticMethod('GameTools', 'gameRecharge:', data);
    app.getAppConfig = () => jsb.reflection.callStaticMethod('GameTools', 'getAppConfig');
    app.appLog = (data) => jsb.reflection.callStaticMethod('GameTools', 'log:', data);
    app.setItem = (key, value) => jsb.reflection.callStaticMethod('GameTools', 'setItem:value:', key, value);
    app.getItem = (key) => jsb.reflection.callStaticMethod('GameTools', 'getItem:', key);

    TdSdk.onReward = (amount, reason) => jsb.reflection.callStaticMethod('GameTools', 'onReward:reason:', amount, reason);
    TdSdk.onPurchase = (item, number, price) => jsb.reflection.callStaticMethod('GameTools', 'onPurchase:itemNumber:priceInVirtualCurrency:', item, number, price);
    TdSdk.onUse = (item, number) => jsb.reflection.callStaticMethod('GameTools', 'onUse:itemNumber:', item, number);
    TdSdk.onBegin = (mission) => jsb.reflection.callStaticMethod('GameTools', 'onBegin:', mission);
    TdSdk.onCompleted = (mission) => jsb.reflection.callStaticMethod('GameTools', 'onCompleted:', mission);
    TdSdk.onFailed = (mission, cause) => jsb.reflection.callStaticMethod('GameTools', 'onFailed:failedCause:', mission, cause);

} else if (window.jsb && cc.sys.os === cc.sys.OS_ANDROID) {

    app.callLogin = () => jsb.reflection.callStaticMethod(ANDROID_CLASS, 'callLogin', '()V');
    app.onLogout = () => jsb.reflection.callStaticMethod(ANDROID_CLASS, 'onLogout', '()V');
    app.userBind = () => jsb.reflection.callStaticMethod(ANDROID_CLASS, 'userBind', '()V');
    app.openGame = (game) => jsb.reflection.callStaticMethod(ANDROID_CLASS, 'openGame', '(Ljava/lang/String;)V', game);
    app.gameCharge = (data) => jsb.reflection.callStaticMethod(ANDROID_CLASS, 'gameRecharge', '(Ljava/lang/String;)V', data);
    app.getAppConfig = () => jsb.reflection.callStaticMethod(ANDROID_CLASS, 'getAppConfig', '()Ljava/lang/String;');
    app.appLog = (data) => jsb.reflection.callStaticMethod(ANDROID_CLASS, 'log', '(Ljava/lang/String;)V', data);
    app.setItem = (key, value) => jsb.reflection.callStaticMethod(ANDROID_CLASS, 'setItem', '(Ljava/lang/String;Ljava/lang/String;)V', key, value);
    app.getItem = (key) => jsb.reflection.callStaticMethod(ANDROID_CLASS, 'getItem', '(Ljava/lang/String;)Ljava/lang/String;', key);

    TdSdk.onReward = (amount, reason) => jsb.reflection.callStaticMethod(ANDROID_CLASS, 'onReward', '(FLjava/lang/String;)V', amount, reason);
    TdSdk.onPurchase = (item, number, price) => jsb.reflection.callStaticMethod(ANDROID_CLASS, 'onPurchase', '(Ljava/lang/String;IF)V', item, number, price);
    TdSdk.onUse = (item, number) => jsb.reflection.callStaticMethod(ANDROID_CLASS, 'onUse', '(Ljava/lang/String;I)V', item, number);
    TdSdk.onBegin = (mission) => jsb.reflection.callStaticMethod(ANDROID_CLASS, 'onBegin', '(Ljava/lang/String;)V', mission);
    TdSdk.onCompleted = (mission) => jsb.reflection.callStaticMethod(ANDROID_CLASS, 'onCompleted', '(Ljava/lang/String;)V', mission);
    TdSdk.onFailed = (mission, cause) => jsb.reflection.callStaticMethod(ANDROID_CLASS, 'onFailed', '(Ljava/lang/String;Ljava/lang/String;)V', mission, cause);

} else {

    app.callLogin = () => cc.Proxy.getToken(data => cc.Proxy.onUserLogin(data));
    app.onLogout = () => cc.log('app onLogout');
    app.userBind = () => cc.log('app userBind');
    app.openGame = (game) => cc.Emitter.emit('EVT_OPEN_GAME', game);
    app.gameCharge = (data) => { setTimeout(function () { cc.Proxy.onGameRecharge({ state: 0 }) }, 2000); };
    app.getAppConfig = () => { if (cc.sys.isMobile) CONFIG.baseUrl = ''; return JSON.stringify(CONFIG); };
    app.appLog = (data) => { cc.log(data) };
    app.setItem = (key, value) => { cc.log('set item', key, value) };
    app.getItem = (key) => { };

    TdSdk.onReward = (amount, reason) => { cc.log('TdSdk.onReward', amount, reason) };
    TdSdk.onPurchase = (item, number, price) => { cc.log('TdSdk.onPurchase', item, number, price) };
    TdSdk.onUse = (item, number) => { cc.log('TdSdk.onUse', item, number) };
    TdSdk.onBegin = (mission) => { cc.log('TdSdk.onBegin', mission) };
    TdSdk.onCompleted = (mission) => { cc.log('TdSdk.onCompleted', mission) };
    TdSdk.onFailed = (mission, cause) => { cc.log('TdSdk.onFailed', mission, cause) };
}

window.TdSdk = TdSdk;

module.exports = app;